import 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import VueLazyload from 'vue-lazyload'
import router from './routes'
import store from './store'
import './common/filter'
import './components'
import 'css/base.scss'
import '../static/jquery.form'
import { session, parseDecryptParams } from './common/util'

Vue.use(VueLazyload, {
  error: require('./assets/noimg.png'),
  loading: require('./assets/noimg.png')
})

Vue.config.productionTip = false

// 判断登录
router.beforeEach((to, from, next) => {
  // 如果路由中有加密的用户信息数据，则从路由中获取加密数据，并转义（放到session中）
  parseDecryptParams(to.query.key)

  // 等js获取cookie数据后处理数据
  setTimeout(() => {
    if (to.meta.isAuth) {
      const userInfo = session.get('userInfo') || {}
      if (userInfo.accessToken) {
        next()
      } else {
        next({
          path: '/login'
        })
      }
    } else {
      next()
    }
  }, 5)
})

// 初始化JMessage
window.JIM = new JMessage({
  // debug: true
})
store.dispatch('JIMInit', {})

export default new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
